﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.FindArtInfo;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.RegInfo;
using BusinessLayer.RegInfo.Entities;

namespace BackEnd.Controllers
{
    public class AdvertiseController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateAdvertise)]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateAdvertise)]
        public ActionResult Create(AdvertiseModel advertiseModel)
        {            
            if (ModelState.IsValid==false)
            {
                ModelState.AddModelError("MessageResult", "Error");
                return View();
            }
            else
            {
                new AdvertiseBL().Insert(new Advertise()
                                           {
                                               Title = advertiseModel.Title,
                                               EndDate = advertiseModel.EndDate,
                                               ImageName = advertiseModel.ImageName,
                                               StartDate = advertiseModel.StartDate,
                                               UrlRedirect = advertiseModel.UrlRedirect
                                           });
                ModelState.AddModelError("MessageResult", "Success");
                return RedirectToAction("Index", "Advertise");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditAdvertise)]
        public ActionResult Edit(int id)
        {
            AdvertiseModel model = new AdvertiseModel();
            Advertise advertise = new AdvertiseBL().GetById(id);
            if (advertise != null)
            {
                model.Id = advertise.Id;
                model.Title = advertise.Title;
                model.StartDate = advertise.StartDate;
                model.EndDate = advertise.EndDate;
                model.ImageName = advertise.ImageName;
                model.UrlRedirect = advertise.UrlRedirect;
            }
            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditAdvertise)]
        public ActionResult Edit(AdvertiseModel advertiseModel)
        {            
            if (ModelState.IsValid == false)
            {
                ModelState.AddModelError("MessageResult", "Error");
                return View();
            }
            else
            {
                Advertise advertise = new AdvertiseBL().GetById(advertiseModel.Id);
                advertise.Title = advertiseModel.Title;
                advertise.StartDate = advertiseModel.StartDate;
                advertise.EndDate = advertiseModel.EndDate;
                advertise.ImageName = advertiseModel.ImageName;
                advertise.UrlRedirect = advertiseModel.UrlRedirect;
                new AdvertiseBL().Update(advertise);
                ModelState.AddModelError("MessageResult", "Success");
                return RedirectToAction("Index", "Advertise");
            }
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteAdvertise)]
        public JsonResult Delete(string listId)
        {
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new AdvertiseBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteAdvertise)]
        public JsonResult GetAll(int pageIndex, int pageSize)
        {
            int totalRecord;
            List<Advertise> listAcdAdvertises = new AdvertiseBL().GetAll(pageIndex, pageSize, out totalRecord);
            ListObjectModel<Advertise> listObject = new ListObjectModel<Advertise>
            {
                ListItem = listAcdAdvertises,
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecord = totalRecord
            };
            return Json(listObject, JsonRequestBehavior.AllowGet);
        }

    }
}
